import 'package:flutter_app/utils/video_compress_utils.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image_pickers/image_pickers.dart';

export 'package:image_picker/image_picker.dart';

/**
 * @Description: image_picker拍照  video_compress视频压缩 缩略图
 * @Author: 下雨了
 * @CreateDate: 2021-12-08 11:01
 * @UpdateUser: 更新者
 * @UpdateDate:  2021-12-08 11:01
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
//定义一个top-level（全局）变量，
final imagePicker = ImagePickerUtils();

// enum GalleryMode {
//   ///选择图片
//   image,
//
//   ///选择视频
//   video,
// }
//
// class Media {
//   ///视频缩略图图片路径
//   ///Video thumbnail image path
//   String? thumbPath;
//
//   ///视频路径或图片路径
//   ///Video path or image path
//   String? path;
//   GalleryMode? galleryMode;
// }

class ImagePickerUtils {
  late ImagePicker _picker;

  //私有构造函数
  ImagePickerUtils._internal() {
    _picker = new ImagePicker();
  }

  //保存单例
  static ImagePickerUtils _imagePickerUtils = ImagePickerUtils._internal();

  //工厂构造函数
  factory ImagePickerUtils() {
    return _imagePickerUtils;
  }

  // 打开相机 拍摄照片
  Future<Media?> openCamera() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.camera, imageQuality: 100);
    if (image != null) {
      Media media = new Media();
      media.path = image.path;
      media.thumbPath = "";
      media.galleryMode = GalleryMode.image;
      return media;
    }
  }

  // 打开相机 拍摄视频
  Future<Media?> openCameraVideo() async {
    final XFile? video = await _picker.pickVideo(
        source: ImageSource.camera,
        preferredCameraDevice: CameraDevice.rear,
        maxDuration: Duration(seconds: 300)
    );

    if (video == null) {
      return null;
    }

    /// 使用压缩 开始
    // Loading.show(status: '视频压缩中');
    // / 使用 video_compress 对视频 进行压缩
    // log.i("压缩前的时间 = " + DateTime.now().toString());
    // 2022-02-10 15:19:24.624584
    // MediaInfo? mediaInfo = await VideoCompressUtils().compressVideo(
    //     video.path,
    // );
    // log.i(mediaInfo.toString());
    // final thumbnailFile = await VideoCompressUtils().getFileThumbnail(
    //     mediaInfo!.path!,
    //     quality: 80, // default(100)
    //     position: -1 // default(-1)
    // );
    // Loading.dismiss();
    // log.i(DateTime.now().toString());
    // 2022-02-10 15:19:30.050911
    // Media media = new Media();
    // media.path = mediaInfo.path;
    // media.thumbPath =  thumbnailFile.path;
    // media.galleryMode = GalleryMode.video;
    /// 使用压缩 结束

    /// 不使用压缩 开始
    // 获取视频缩略图
    final thumbnailFile = await VideoCompressUtils().getFileThumbnail(video.path);
    Media media = new Media();
    media.path = video.path;
    media.thumbPath = thumbnailFile.path;
    media.galleryMode = GalleryMode.video;
    /// 不使用压缩 结束

    return media;
  }

  Future<List<XFile>?> pickMultiImage({
    double? maxWidth,
    double? maxHeight,
    int? imageQuality,
    bool requestFullMetadata = true,
  }) async {
    final List<XFile>? image = await _picker.pickMultiImage(
      maxWidth: maxWidth,
      maxHeight: maxHeight,
      imageQuality: imageQuality,
      requestFullMetadata: requestFullMetadata,
    );
    return image;
  }

  Future<XFile?> pickImage({
    required ImageSource source,
    double? maxWidth,
    double? maxHeight,
    int? imageQuality,
  }) async {
    XFile? image = await ImagePicker().pickImage(
      source: source,
      maxWidth: maxWidth,
      maxHeight: maxHeight,
      imageQuality: imageQuality,
    );
    return image;
  }

  Future<XFile?> pickVideo({
    required ImageSource source,
    CameraDevice preferredCameraDevice = CameraDevice.rear,
    Duration? maxDuration,
  }) async {
    XFile? video = await ImagePicker().pickVideo(
      source: source,
      preferredCameraDevice: preferredCameraDevice,
      maxDuration: maxDuration,
    );
    return video;
  }
}
